﻿package com.purplemovies.games4nickie.deadly_dots_1
{
	import com.purplemovies.games4nickie.deadly_dots_1.data.*
	import com.purplemovies.games4nickie.deadly_dots_1.display.*
	import com.purplemovies.events.GameEvent;
	
	import flash.system.fscommand;
	import flash.system.Capabilities;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	
	import flash.display.Sprite;
	public class Game extends Sprite {
		
		public var deaddots	:int;			// dead dots.
		public var l_num	:int;			// Dots on screen during level.
		
		public var dots		:Array;			// Dots on screen during level.
		public var levels	:Array;			// List of level data.
		public var level	:Level;			// The current level.
		public var score	:int;			// Player score.
		public var main_tf	:TextField;		// Main Text field for displaying score.
		
		public var screen_width		:int; 
		public var screen_height	:int; 
		
		function Game(){
			init();
		}
		
		public function init(){
			
			fscommand( 'fullscreen', 'true' );
			screen_width	= Capabilities.screenResolutionX;
			screen_height	= Capabilities.screenResolutionY;
			
			// Draw BG color:
			var bg_x	= Capabilities.screenResolutionX - stage.stageWidth;
			var bg_y	= Capabilities.screenResolutionY - stage.stageHeight;
			graphics.beginFill( 0x111122 );
			graphics.drawRect( -bg_x, -bg_y, Capabilities.screenResolutionX+bg_x, Capabilities.screenResolutionY+bg_y );
			
			// Setup Text field:
			main_tf						= new TextField();
			var text_format:TextFormat	= new TextFormat( new FBot().fontName, 60, 0x00ff00 );
			
			main_tf.defaultTextFormat	= text_format;
			main_tf.width				= stage.stageWidth;
			main_tf.embedFonts			= true;
			main_tf.selectable			= false;
			main_tf.antiAliasType		= AntiAliasType.ADVANCED;
			main_tf.autoSize			= 'center';
			main_tf.text				= 'Deadly Dots!!!!!';
			addChild( main_tf );

			// Setup Levels:
			levels = [
					  new Level(1, 20),
					  new Level(2, 20),
					  new Level(3, 20),
					  new Level(4, 20),
					  new Level(5, 20),
					  new Level(6, 20)
					  ];
			
			l_num		= 0;
			
			startLevel( l_num );
		}
		
		public function startLevel( pLevI:int ){
			deaddots	= 0;
			
			dots 		= [];
			level		= levels[pLevI];
			for( var i=0; i < level.dots_number; i++ ){
				dots[i] = new DeadlyDot( ( Math.random() * (stage.stageWidth-40)	) + 20,
											( Math.random() * (stage.stageHeight-40)	) + 20,
																					 level.dots_radius, 0xff0000, 30 );
				dots[i].addEventListener( MouseEvent.CLICK, onDotClicked );
				dots[i].addEventListener( GameEvent.DIED, onDotDied );
				//dots[i].addEventListener( GameEvent.DIED, onDotDied );
				
				// GameEvent.DIED
				addChild( dots[i] );
			}
			//addEventListener( Event.ENTER_FRAME, onFrame );
		}
		
		private function onDotDied(e){
			
			trace('arse');
			
			trace('arse   3');
			
			deaddots -= 1;
			
			if( deaddots <= 0 ){
				startLevel( pLevI+1 );
			}
		}
		
		private function onDotClicked(e){
			e.target.die();
			
			l_num++;
			
			//startLevel( l_num );
			
			//removeChild( e.target );
			// Add to score.
		}
		
		
	}
}










